﻿using BadOCR.Runtime;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BadOCR.Common
{
    public class SpaceOCR
    {
        private string _apiKey;

        public SpaceOCR(string apiKey)
        {
            _apiKey = apiKey;
        }

        /// <summary>
        /// http://ocr.space/OCRAPI
        /// </summary>
        /// <param name="imageData"></param>
        /// <param name="languageType"></param>
        /// <returns></returns>
        public async Task<string> Generic(byte[] imageData, LanguageKind languageKind = LanguageKind.SimplifiedChinese)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, 20);

            MultipartFormDataContent form = new MultipartFormDataContent();
            // "5a64d478-9c89-43d8-88e3-c65de9999580"
            form.Add(new StringContent(_apiKey), "apikey"); //Added api key in form data

            var language = LanguageHelper.GetSpaceOCRLanguageCode(languageKind);
            form.Add(new StringContent(language), "language");

            //form.Add(new StringContent("2"), "ocrengine"); //  默认为1
            form.Add(new StringContent("true"), "scale");
            form.Add(new StringContent("true"), "isTable");

            //if (string.IsNullOrEmpty(ImagePath) == false)
            //{
            //    byte[] imageData = File.ReadAllBytes(ImagePath);
            form.Add(new ByteArrayContent(imageData, 0, imageData.Length), "image", "image.jpg");
            //}
            //else if (string.IsNullOrEmpty(PdfPath) == false)
            //{
            //    byte[] imageData = File.ReadAllBytes(PdfPath);
            //    form.Add(new ByteArrayContent(imageData, 0, imageData.Length), "PDF", "pdf.pdf");
            //}

            HttpResponseMessage response = await httpClient.PostAsync("https://api.ocr.space/Parse/Image", form);
            string strContent = await response.Content.ReadAsStringAsync();
            Rootobject ocrResult = JsonConvert.DeserializeObject<Rootobject>(strContent);

            var sb = new StringBuilder();
            if (ocrResult.OCRExitCode == 1)
            {
                for (int i = 0; i < ocrResult.ParsedResults.Count(); i++)
                {
                    sb.Append(ocrResult.ParsedResults[i].ParsedText);
                }
            }
            return sb.ToString();
        }

        class Rootobject
        {
            public Parsedresult[] ParsedResults { get; set; }
            public int OCRExitCode { get; set; }
            public bool IsErroredOnProcessing { get; set; }
            public string ErrorMessage { get; set; }
            public string ErrorDetails { get; set; }
        }

        class Parsedresult
        {
            public object FileParseExitCode { get; set; }
            public string ParsedText { get; set; }
            public string ErrorMessage { get; set; }
            public string ErrorDetails { get; set; }
        }
    }
}
